package com.jilefojing.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import com.jilefojing.beans.enums.ItemTypeEnumForSource;
import com.jilefojing.beans.pojos.RHotComment;
import com.jilefojing.beans.query.RHotCommentQuery;
import com.jilefojing.beans.vo.Pagination;
import com.jilefojing.dao.IRHotCommentDao;
import com.jilefojing.dao.base.impl.BaseDao;
import com.jilefojing.dao.mapper.IRHotCommentMapper;
import com.jilefojing.util.Detect;

/**
 * 
 * @author 作者 jgshun
 * @date 创建时间 2016年07月21日 21时16分46秒
 */
@Repository
public class RHotCommentDao extends BaseDao<RHotComment> implements IRHotCommentDao {
	@Autowired
	private IRHotCommentMapper rHotCommentMapper;

	@Override
	public Pagination<RHotComment> getPagination(RHotCommentQuery rHotCommentQuery, Pagination<RHotComment> pagination) {
		Example example = new Example(RHotComment.class);
		Criteria criteria = example.createCriteria();

		if (Detect.notEmpty(rHotCommentQuery.getIds())) {
			criteria.andIn(RHotComment.FIELD_ID, rHotCommentQuery.getIds());
		}

		if (Detect.notEmpty(rHotCommentQuery.getItemTypes())) {
			List<Short> itemTypeCodes = new ArrayList<Short>();
			for (ItemTypeEnumForSource _ItemType : rHotCommentQuery.getItemTypes()) {
				itemTypeCodes.add(_ItemType.getCode());
			}
			criteria.andIn(RHotComment.FIELD_ITEMTYPE, itemTypeCodes);
		}

		if (Detect.notEmpty(rHotCommentQuery.getItemIds())) {
			criteria.andIn(RHotComment.FIELD_ITEMID, rHotCommentQuery.getItemIds());
		}

		if (Detect.notEmpty(rHotCommentQuery.getCommentIds())) {
			criteria.andIn(RHotComment.FIELD_COMMENTID, rHotCommentQuery.getCommentIds());
		}

		if (Detect.notEmpty(rHotCommentQuery.getCreateUserIds())) {
			criteria.andIn(RHotComment.FIELD_CREATEUSER, rHotCommentQuery.getCreateUserIds());
		}

		if (rHotCommentQuery.getEqualCreateTime() != null) {
			criteria.andEqualTo(RHotComment.FIELD_CREATETIME, rHotCommentQuery.getEqualCreateTime());
		}

		if (rHotCommentQuery.getEqualAndBeforCreateTime() != null) {
			criteria.andGreaterThanOrEqualTo(RHotComment.FIELD_CREATETIME, rHotCommentQuery.getEqualAndBeforCreateTime());
		}

		if (rHotCommentQuery.getEqualAndAfterCreateTime() != null) {
			criteria.andLessThanOrEqualTo(RHotComment.FIELD_CREATETIME, rHotCommentQuery.getEqualAndAfterCreateTime());
		}

		if (rHotCommentQuery.getBeforCreateTime() != null) {
			criteria.andGreaterThan(RHotComment.FIELD_CREATETIME, rHotCommentQuery.getBeforCreateTime());
		}

		if (rHotCommentQuery.getAfterCreateTime() != null) {
			criteria.andLessThan(RHotComment.FIELD_CREATETIME, rHotCommentQuery.getAfterCreateTime());
		}

		if (Detect.notEmpty(pagination.getOrders())) {
			example.setOrderByClause(pagination.getOrdersToStr());
		}

		return super.getPagination(pagination, example);
	}
}
